package sg.com.techstudio.cfoconnect.dao;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import sg.com.techstudio.cfoconnect.email.GenericHTMLPreparator;
import sg.com.techstudio.cfoconnect.obj.RegisterObj;
import sg.com.techstudio.cfoconnect.util.NumberToolSg;

@Component
public class EmailDao {
	private static final Log log = LogFactory.getLog(EmailDao.class);
	@Autowired
	private VelocityEngine velocityEngine;
	
	@Autowired
	private JavaMailSender mailSender;
	
	@Autowired
	private EmailConfig confirmationEmailConfig
		, acknowledgementEmailConfig;
	
	public boolean sendConfirmationEmail(RegisterObj r){
		GenericHTMLPreparator emailPrep = confirmationEmailPrep(r);
		Map<String, Object> emailInfoMap = new HashMap<String, Object>();
		emailInfoMap.put("r", r);
		emailInfoMap.put("number", new NumberToolSg());
		emailPrep.setModel(emailInfoMap);
		mailSender.send(emailPrep);
		return true;
	}
	
	public boolean sendEmail(RegisterObj registerObj){
		log.debug("sendEmail() entry");
		boolean ret = sendConfirmationEmail(registerObj);
		if(!StringUtils.isBlank(registerObj.getPaEmail()) 
				&& !StringUtils.isBlank(registerObj.getPaName())) {
			//resend to pa
			registerObj.setFirstName(registerObj.getPaName());
			registerObj.setEmail(registerObj.getPaEmail());
			ret&=sendConfirmationEmail(registerObj);
		}
		return ret;
	}
	
	protected GenericHTMLPreparator confirmationEmailPrep(RegisterObj r) {
		GenericHTMLPreparator emailPrep = new GenericHTMLPreparator();
		emailPrep.setVelocityEngine(velocityEngine);
		emailPrep.setFrom(confirmationEmailConfig.getFrom());
		emailPrep.setTo(r.getEmail());
//		if(!StringUtils.isBlank(r.getPaEmail()))
//			emailPrep.setCc(r.getPaEmail());
//		emailPrep.setTo(TEST_EMAIL);
		emailPrep.setBcc(confirmationEmailConfig.getBcc());
		emailPrep.setSubject(confirmationEmailConfig.getSubject());
		emailPrep.setSenderName(confirmationEmailConfig.getSenderName());
		emailPrep.setTemplateLocation(confirmationEmailConfig.getTemplateLocation());
		return emailPrep;
	}
	
	
	//for sending email manually
	
	protected static final String TEST_EMAIL = "naz@roxwell.com.sg";
	private static EmailDao emailDao;
	private static String configLocation = "config/servlet-context.xml";
	public static void main(String[] args) {
		System.out.println("start");
		try {
			if(args!=null && args.length>=1)
				configLocation = args[0];
			initProcess(configLocation);
//			RegisterObj r = registrationDao.getRegisterObj(1);
			
//			emailDao.sendParticipantEmail(getP(), getR());
//			emailDao.sendOtherCompanyEmail(getR());
//			emailDao.sendChequeEmail(getR());
			emailDao.sendEmail(getR());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("EMailDao", e);
		}
		System.out.println("end");
	}
	
	protected static RegisterObj getR() {
		RegisterObj r = new RegisterObj();
		r.setFirstName("firsnm");
		r.setLastName("lastnm");
		r.setFullName("naz name");
		r.setDesignation("naz designation");
		r.setCompanyName("roxwell");
		r.setId(19848L);
		r.setEmail(TEST_EMAIL);
		r.setDietary("");
		r.setPaName("pa name aa");
		r.setPaEmail("dhany.basuki@techstudio.com.sg");
		//r.setDietary("vegetarian");
		return r;
	}
	
	@SuppressWarnings("resource")
	protected static void initProcess(String configLocation) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
		emailDao = ctx.getBean(EmailDao.class);
	}
}
